package com.le.tester.javaThink.fifth.chapter18_Str;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * createTime：2021/8/13 15:43
 * description：matcher 中的start and end
 */
public class StartEnd {
    public static String input = "As long as there is injustice, whenever a\\n\" +     \n" +
            "      \"Targathian baby cries out, wherever a distress\\n\" +     \n" +
            "      \"signal sounds among the stars \" +     \n" +
            "      \"... We'll be there.\\n\"+     \n" +
            "      \"This fine ship, and this fine crew ...\\n\" +     \n" +
            "      \"Never give up! Never surrender!";

    private static class Display {
        private boolean regexPrinted = false;
        private String regex;

        public Display(String regex) {
            this.regex = regex;
        }

        void display(String msg) {
            if (!regexPrinted) {
                System.out.println(regex);
                regexPrinted = true;
            }
            System.out.println(msg);
        }
    }

    static void examine(String s, String regex) {
        Display display = new Display(regex);
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()) {
            display.display("find() " + matcher.group() + " start()=" + matcher.start() + " end()=" + matcher.end());

        }
        if (matcher.lookingAt()) {
            display.display("lookAt() start=" + matcher.start() + " end=" + matcher.end());
        }
        if (matcher.matches()) {
            display.display("matchers() start=" + matcher.start() + " end " + matcher.end());
        }
    }

    public static void main(String[] args) {
        for (String in : input.split("\n")) {
            System.out.println("input :" + in);
            for (String regex : new String[]{"\\w*ere\\w*",
                    "\\w*ever", "T\\w+", "Never.*?!"}) {
                examine(in, regex);
            }
        }
    }

}
